package com.javasm.controller.JINXIAOController;

import com.javasm.commons.basic.BaseController;
import com.javasm.commons.config.TaskConfigurer;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.entity.JINXIAOEntity.SysTasks;
import com.javasm.service.JINXIAOService.SysTasksService;
import com.javasm.task.async.AsynTaskManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("sysTask")
public class SysTaskController extends BaseController {
    private static Logger l = LoggerFactory.getLogger(SysTaskController.class);

    @Resource
    private SysTasksService sysTasksService;

    @Resource
    private TaskConfigurer taskConfigurer;

    //停止任务
    @GetMapping("stop/{id}")
    public AxiosResult stopTask(@PathVariable String id){
        //1.把任务停止
        taskConfigurer.stop(id);
        //2,修改数据库任务状态:stop
        sysTasksService.changeStatus(id,"stop");
        return suc("成功");
    }

    //启动任务
    @GetMapping("start/{id}")
    public AxiosResult startTask(@PathVariable String id){
        //0.查找任务
        SysTasks task = sysTasksService.queryById(id);
        //1.把任务启动
        taskConfigurer.start(task);
        //2,修改数据库任务状态:running
        sysTasksService.changeStatus(id,"running");
        return suc("成功");
    }

    //执行一次任务
    @GetMapping("execute/{id}")
    public AxiosResult executeTask(@PathVariable String id){
        //0.查找任务
        SysTasks task = sysTasksService.queryById(id);
        Runnable runnable = taskConfigurer.createRunnable(task.getTaskClass());
        AsynTaskManager.execute(runnable);
        return suc("成功");
    }

    @GetMapping("{id}")
    public AxiosResult selectById(@PathVariable Integer id)
    {
        SysTasks obj = this.sysTasksService.queryById(id);
        return suc(obj);
    }
    
//    @GetMapping("ids/{ids}")
//    public AxiosResult selectListByIds(@PathVariable String ids)
//    {
//        String[] idsArray = ids.split(",");
//        List<SysTasks> list = this.sysTasksService.listByIds(Arrays.asList(idsArray));
//        return suc(list);
//    }

//    @GetMapping("list")
//    public TableDatas selectList(SysTasks obj)
//    {
//        startPage();
//        QueryWrapper w = new QueryWrapper(obj);
//        List<SysTasks> list = this.sysTasksService.list(w);
//        return toTableDatas(list);
//    }

//    @PostMapping
//    public AxiosResult add(@RequestBody SysTasks obj)
//    {
//        Boolean r = this.sysTasksService.save(obj);
//        return isok(r);
//    }

    @PutMapping
    public AxiosResult update(@RequestBody SysTasks obj)
    {
        Boolean r = this.sysTasksService.updateById(obj.getId());
        return isok(r);
    }

//    @DeleteMapping("{ids}")
//    public AxiosResult delByIds(@PathVariable String ids)
//    {
//        String[] idsArray = ids.split(",");
//        Boolean r = this.sysTasksService.removeByIds(Arrays.asList(idsArray));
//        return isok(r);
//    }

}